package com.loop.homework;

public class Day_10 {
    public static void main(String[] args) {

        /*findO();
        String str = "Hello12345World";
        System.out.println(changeLowOrUpper(str));*/
        //findWord();
    }

    /**
     * 找出字符串”o“的每次出现的位置
     */
    public static void findO(){
        String str = "HelloWord";
        int i=0;
        for (;;) {
            i = str.indexOf("o", i);
            if (i<0)
                return;
            System.out.println(i);
            i++;
        }
    }

    /**
     * 2.分析以下需求，并用代码实现
     *
     * (1)定义字符串Hello12345World
     *
     * (2)将字符串中大写字母变成小写字母，小写字母变成大写字母，其他的每一个字符用"*"代替
     *
     * 		举例:
     *
     * 	输出结果：hELLO*****wORLD
     */
    public static String changeLowOrUpper(String str){
        char[] chars = str.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 'a'&& chars[i] <= 'z'){
                stringBuilder.append( (chars[i]+"").toUpperCase() );
            }else if (chars[i] >= 'A'&& chars[i] <= 'Z'){
                stringBuilder.append( (chars[i]+"").toLowerCase() );
            }else {
                stringBuilder.append("*");
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 3.定义出字符串 “Hi If you want to change your life , you must work hard !”, 将该字符串中,含有两个
     *
     * 字母的单词获取出来, 并计算有多少个
     */
    public static void findWord(){
        String str="Hi If you want to change your life , you must work hard !";
        String[] s = str.split(" ");
        int count =0;
        System.out.println("含有两个字母的单词有：");
        for (int i = 0; i < s.length; i++) {
            if (s[i].length()==2){
                System.out.print(s[i]+" ");
                count++;
            }
        }
        System.out.println();
        System.out.println("一共"+count+"个");
    }
}
